<?php
/**
 * PatientSignalSearch model class file.
 *
 * @package   models
 * @since     1.0
 * @filesource
 */

/**
 * The model for the signal search form. Used in display signals for user.
 * 
 * PatientSignalSearch is a Form class. 
 * It is the data structure for keeping search form data. It is used by 
 * PatientSignalController.actionAdmin.
 */
class PatientSignalSearch extends CFormModel {

	/**
	 * Subject to search with
	 * @var string
	 */
	public $subject;
	
	/**
	 * The min age to search with
	 * @var int
	 */
	public $ageMin;
	
	/**
	 * The max age to search with
	 * @var int
	 */
	public $ageMax;
	
	/**
	 * The gender of patient to search with
	 * @var int
	 * @see DataUtil::getGender()
	 */
	public $gender;
	
	/**
	 * The disorder id to search with. On the form it is the disorder drop down where the id is submitted.
	 * @var int
	 */
	public $disorderId;
	
	/**
	 * The organization id to search with. On the form it is the organization drop down where the id is submitted.
	 * @var unknown_type
	 */
	public $organizationId;
	
	/**
	 * The min record date to search with. In the form "yyyy-mm-dd"
	 * @var string
	 */
	public $recordingDateMin;
	
	/**
	 * The max record date to search with. In the form "yyyy-mm-dd"
	 * @var string
	 */
	public $recordingDateMax;

	/**
	 * Declares the validation rules.
	 * The rules will be used when requesting validation by call self.validate().
	 * Many class method will call self.validate(), eg: CActiveRecord.save().
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			// following should be integer
			array('ageMin, ageMax, gender, disorderId, organizationId', 'numerical', 'integerOnly'=>true),
			array('subject', 'length', 'max'=>255),
			array('recordingDateMin, recordingDateMax', 'safe'),
			// the subject name of patient signal must be letters, digit, underscroes and dash.
			array('subject', 'match', 'pattern'=>'/^[0-9a-zA-z_\-]+$/','message'=>'{attribute} is invalid. Must be letters and digits only.'),		
		);
	}

	/**
	 * Returns the labels for each column variables.
	 * This will be used to render label on page.
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'subject' => 'Subject Identifier',
			'ageMin' => 'Age',
			'gender' => 'Gender',
			'disorderId' => 'Disorder',
			'organizationId' => 'Organization',
			'createDate' => 'Create Date',
			'recordingDateMin' => 'Recording Date',
		);
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		return new CActiveDataProvider('PatientSignal', array(
			'criteria'=>$this->createCriteria(),
		));
	}
	
	/**
	 * Create criteria for the functionalbity of searching patient signal results on index page of patient signal.
	 * @throws CHttpException if user cannot access the organization
	 * @return CDbCriteria the CDbCriteria to match this search
	 */
	public function createCriteria(){
		$criteria=new CDbCriteria;

		$criteria->compare('subject',$this->subject,true);
		$criteria->compare('ageYear','>='.$this->ageMin);
		$criteria->compare('ageYear','<='.$this->ageMax);
		$criteria->compare('gender',$this->gender);
		$criteria->compare('disorderId',$this->disorderId);
		
		if(!empty($this->recordingDateMin)){
			$criteria->compare('recordingTime','>='.$this->recordingDateMin.' 00:00:00');
		}
		if(!empty($this->recordingDateMax)){
			$criteria->compare('recordingTime','<='.$this->recordingDateMax.' 23:59:59');
		}

		// Patient signal search is based on organization.
		if($this->organizationId>0){
			// Check if user has view access right to the organization.
			if(!Yii::app()->user->checkAccess('signalView',array('organizationId'=>$this->organizationId))){
				throw new CHttpException(400,'You are not permitted to view signals.');
			}
			$criteria->compare('organizationId',$this->organizationId);
		}else{
			// if the organization of the patient siganl is not specified, return empty result.
			$criteria->addCondition('0=1');
		}
		// Join tables of organization and disorder.
		$criteria->with=array('organization','disorder');
		return $criteria;
	}
}